package org.example;

public class Test2 {
    //leetcode 135 分发糖果 https://leetcode.cn/problems/candy/description/?envType=study-plan-v2&envId=top-interview-150
    public int candy(int[] ratings) {
        //正向和反向遍历数组一次，没有遍历就填写当前遍历顺序较大的孩子分得的糖果
        int n = ratings.length;
        int[] tmp = new int[n];
        tmp[0] = 1;
        for (int i = 1; i < n; i++) {
            tmp[i] = ratings[i] > ratings[i-1] ? tmp[i-1]+1 : 1;
        }
        for (int i = n-2; i >= 0; i--) {
            if (ratings[i] > ratings[i+1]) {
                tmp[i] = Math.max(tmp[i],tmp[i+1] + 1);
            }
        }
        int ret = 0;
        for (int count : tmp) ret += count;
        return ret;
    }
}
